package controller

import (
	"context"
	"grpc_demo/internal/converter"
	"grpc_demo/internal/model"
	"grpc_demo/internal/validator"
	"grpc_demo/pkg/logger"
	pb "grpc_demo/proto_gen"
)

func LoginController(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) {
	if err := validator.LoginValidator(in); err != nil {
		logger.WithContext(ctx).Errorf("Login Validator Error %v", err)
		return nil, err
	}
	loginReq := converter.Login2LoginReq(in)
	loginResp, err := model.LoginModel(ctx, loginReq)
	if err != nil {
		logger.WithContext(ctx).Errorf("Login  Error %v", err)
		return nil, err
	}
	out := converter.Login2LoginResp(loginResp)
	logger.WithContext(ctx).Infof("Login Exit StuId:%s , Message :%s , Code: %d", in.StuId, out.Message, out.Code)
	return out, nil
}
